import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  static const _basicMessageChannel =
      BasicMessageChannel<dynamic>('numberTest', StandardMessageCodec());
  static const _basicMessageChannel2 =
      BasicMessageChannel<dynamic>('objectTest', StandardMessageCodec());

  Map<String?, Object?> convertMap(Map<Object?, Object?> original) {
    Map<String?, Object?> newMap = {};
    original.forEach((key, value) {
      if (key != null) {
        newMap[key.toString()] = value;
      } else {
        newMap[null] = value;
      }
    });
    return newMap;
  }

  static const String arg_key = 'bigint_key';
  int arg_value = 9223372036854775807;
  Future<void> _incrementCounter() async {

    // 使用 then 确保在异步操作完成后更新状态
    final future1 = _basicMessageChannel.send(arg_value);
    final future2 = _basicMessageChannel2.send(<Object?>[arg_key, arg_value]);
    print('jktest futures created');
    Future.wait([future1, future2]).then((results) {
      print('jktest futures completed');
      try {
        if (results[0] == null || results[1] == null) {
          print('jktest result is null');
          return;
        }
        print('jktest result0 type: ${results[0].runtimeType}');
        print('jktest result0 value: ${results[0]}');
        print('jktest result1 type: ${results[1].runtimeType}');
        print('jktest result1 value: ${results[1]}');

        final int result1 = results[0] as int;
        final Map<Object?, Object?> result2 =
            results[1] as Map<Object?, Object?>;

        print('jktest result1: $result1');
        print('jktest result2: $result2');
        final Map<String?, Object?> stringKeyMap = convertMap(result2);
        print('jktest converted value: ${stringKeyMap["bigint_key"]}');
      } catch (e) {
        print('jktest error in type conversion: $e');
      }
      setState(() {
        arg_value--;
        _counter++;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
